/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aktor;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author WIN7
 */
public class Pasien {

    private String nama;
    private String alamat;
    private String notelp;
    private String tempatlahir;
    private String tgllahir;
    private String ID;
    private int BB;
    private int TB;
    private String riwayatkesehatan;

    /**
     * @return the nama
     */
    public String getNama() {
        return nama;
    }

    /**
     * @param nama the nama to set
     */
    public void setNama(String nama) {
        this.nama = nama;
    }

    /**
     * @return the alamat
     */
    public String getAlamat() {
        return alamat;
    }

    /**
     * @param alamat the alamat to set
     */
    public void setAlamat(String alamat) {
        this.alamat = alamat;
    }

    /**
     * @return the tempatlahir
     */
    public String getTempatlahir() {
        return tempatlahir;
    }

    /**
     * @param tempatlahir the tempatlahir to set
     */
    public void setTempatlahir(String tempatlahir) {
        this.tempatlahir = tempatlahir;
    }

    /**
     * @return the tgllahir
     */
    public String getTgllahir() {
        return tgllahir;
    }

    /**
     * @param tgllahir the tgllahir to set
     */
    public void setTgllahir(String tgllahir) {
        this.tgllahir = tgllahir;
    }

    /**
     * @return the BB
     */
    public int getBB() {
        return BB;
    }

    /**
     * @param BB the BB to set
     */
    public void setBB(int BB) {
        this.BB = BB;
    }

    /**
     * @return the TB
     */
    public int getTB() {
        return TB;
    }

    /**
     * @param TB the TB to set
     */
    public void setTB(int TB) {
        this.TB = TB;
    }

    /**
     * @return the riwayatkesehatan
     */
    public String getRiwayatkesehatan() {
        return riwayatkesehatan;
    }

    /**
     * @param riwayatkesehatan the riwayatkesehatan to set
     */
    public void setRiwayatkesehatan(String riwayatkesehatan) {
        this.riwayatkesehatan = riwayatkesehatan;
    }

    public void insertPasien() throws SQLException {

        Connection con = DataBaseConnection.getDBConnection();

        String query = "insert into pasien (id_pasien, nama_pasien, alamat_pasien, "
                + "tempatlahirpasien, tanggallahirpasien, notelppasien, "
                + "BB_pasien, TB_pasien, riwayat_kesehatan) values (?,?,?,?,to_Date(?,'dd/mm/yy'),?,?,?,?)";

        PreparedStatement st = con.prepareStatement(query);

        st.setString(1, this.ID_Pasien());
        st.setString(2, this.nama);
        st.setString(3, this.alamat);
        st.setString(4, this.tempatlahir);
        st.setString(5, this.tgllahir);
        st.setString(6, this.notelp);
        st.setInt(7, this.BB);
        st.setInt(8, this.TB);
        st.setString(9, this.riwayatkesehatan);

        int x = st.executeUpdate();

        st.close();
        con.close();


    }

    public final String ID_Pasien() {
        try {
            String sql = "select max(ID_PASIEN) from PASIEN";
            Connection con = DataBaseConnection.getDBConnection();
            java.sql.Statement sta = con.createStatement();
            ResultSet rs = sta.executeQuery(sql);

            rs.next();
            String result = rs.getString(1);
            if (result != null) {
                int no = Integer.parseInt(result);
                no++;
                sql = "" + no;
                while (sql.length() <= 3) {
                    sql = "0" + sql;
                }
            } else {
                sql = "0000";
            }
            return sql;
        } catch (SQLException ex) {
            Logger.getLogger(Pasien.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }


    }

    public void getIDPasien(String id) throws SQLException {
        Connection con = DataBaseConnection.getDBConnection();

        String query = "select id_pasien from pasien where id_pasien =?";

        PreparedStatement st = con.prepareStatement(query);

        st.setString(1, id);
//        st.setString(2, this.nama);

        ResultSet rs = st.executeQuery();
        Pasien sdf = new Pasien();
        while (rs.next()) {

            sdf.setID(rs.getString(1));
//            sdf.setNama(rs.getString(2));

        }

//        st.close();
//        return sdf;
    }

    /**
     * @return the notelp
     */
    public String getNotelp() {
        return notelp;
    }

    /**
     * @param notelp the notelp to set
     */
    public void setNotelp(String notelp) {
        this.notelp = notelp;
    }

//    public static void main(String[] args) {
//        try {
//            Pasien s = new Pasien();
//            s.setNama("sss");
//            s.insertPasien();
//        } catch (SQLException ex) {
//            Logger.getLogger(Pasien.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }

    /**
     * @return the ID
     */
    public String getID() {
        return ID;
    }

    /**
     * @param ID the ID to set
     */
    public void setID(String ID) {
        this.ID = ID;
    }

    public boolean LoginPasien(String username, String password)  {
        try {
            Connection conn = new DataBaseConnection().getDBConnection();
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("Select ID_PASIEN,NAMA_PASIEN from PASIEN Where ID_PASIEN = '" + password + "' and NAMA_PASIEN ='" + username + "'");
            if (rs.next()) {
                this.ID = rs.getString(1);
                this.nama = rs.getString(2);
                return true;

            }
        } catch (SQLException ex) {
            Logger.getLogger(Pasien.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
